#!/usr/bin/env python
# -*- coding: utf-8 -*-

from sqlalchemy import Column, Integer, String, Date, DateTime, ForeignKey, JSON
from sqlalchemy.sql import func
from sqlalchemy.orm import relationship
from app.core.database import Base

class User(Base):
    """用户数据模型"""
    __tablename__ = "users"

    id = Column(Integer, primary_key=True, index=True, comment="用户ID")
    username = Column(String, unique=True, index=True, nullable=False, comment="用户名")
    password_hash = Column(String, nullable=False, comment="密码哈希")
    role_id = Column(Integer, ForeignKey("roles.id"), nullable=False, comment="角色ID")
    status = Column(Integer, default=1, comment="状态：1-启用，0-禁用")
    create_time = Column(DateTime(timezone=True), server_default=func.now(), comment="创建时间")
    update_time = Column(DateTime(timezone=True), onupdate=func.now(), comment="更新时间")

class Role(Base):
    """角色数据模型"""
    __tablename__ = "roles"

    id = Column(Integer, primary_key=True, index=True, comment="角色ID")
    name = Column(String, unique=True, nullable=False, index=True, comment="角色名称")
    permissions = Column(JSON, nullable=False, comment="权限列表")
    status = Column(Integer, default=1, comment="状态：1-启用，0-禁用")
    create_time = Column(DateTime(timezone=True), server_default=func.now(), comment="创建时间")
    update_time = Column(DateTime(timezone=True), onupdate=func.now(), comment="更新时间")
    description = Column(String, nullable=True, comment="角色描述")

    # 关联关系
    users = relationship("User", backref="role")

class Department(Base):
    """部门数据模型"""
    __tablename__ = "departments"

    id = Column(Integer, primary_key=True, index=True, comment="部门ID")
    name = Column(String, nullable=False, index=True, comment="部门名称")
    parent_id = Column(Integer, ForeignKey("departments.id", ondelete="SET NULL"), nullable=True, comment="父部门ID")
    manager_id = Column(Integer, ForeignKey("employees.id", ondelete="SET NULL", use_alter=True), nullable=True, comment="部门主管ID")
    status = Column(Integer, default=1, comment="状态：1-启用，0-禁用")
    create_time = Column(DateTime(timezone=True), server_default=func.now(), comment="创建时间")
    update_time = Column(DateTime(timezone=True), onupdate=func.now(), comment="更新时间")
    description = Column(String, nullable=True, comment="部门描述")

class Employee(Base):
    """员工数据模型"""
    __tablename__ = "employees"

    id = Column(Integer, primary_key=True, index=True, comment="员工ID")
    name = Column(String, nullable=False, index=True, comment="姓名")
    gender = Column(Integer, nullable=False, comment="性别：1-男，2-女")
    birth_date = Column(Date, nullable=False, comment="出生日期")
    id_card = Column(String, nullable=True, comment="身份证号")
    phone = Column(String, nullable=False, comment="手机号")
    address = Column(String, comment="住址")
    department_id = Column(Integer, ForeignKey("departments.id", ondelete="SET NULL", use_alter=True), nullable=True, comment="部门ID")
    position = Column(String, nullable=False, comment="职位")
    status = Column(Integer, default=1, comment="状态：1-在职，2-离职，3-试用期")
    photo_url = Column(String, comment="照片URL")
    photo_update_time = Column(DateTime(timezone=True), onupdate=func.now(), comment="照片更新时间")
    emergency_contact = Column(String, comment="紧急联系人")
    emergency_phone = Column(String, comment="紧急联系电话")
    create_time = Column(DateTime(timezone=True), server_default=func.now(), comment="创建时间")
    update_time = Column(DateTime(timezone=True), onupdate=func.now(), comment="更新时间")
    remark = Column(String, comment="备注")

# 添加关系定义
Department.parent = relationship("Department", remote_side=[Department.id], backref="children")
Department.manager = relationship("Employee", foreign_keys=[Department.manager_id], backref="managed_departments")
Department.employees = relationship("Employee", backref="department", foreign_keys=[Employee.department_id])
Role.users = relationship("User", backref="role") 